package com.kermel.andromeda.common.util;

import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;

import com.kermel.andromeda.data.Alarm;
import com.kermel.common.util.StringUtils;

public class BundleUtil {
    public static final Alarm retrieveAlarmFromBundleData(Bundle savedInstanceState, Intent intent) {
        Alarm alarm = null;
        
        if (savedInstanceState == null && intent != null) {
            Object extras = intent.getExtras();
            if (extras != null && extras instanceof Bundle) {
                Bundle bundle = (Bundle)extras;
                Parcelable parcelable = bundle.getParcelable(Alarm.ALARM_PARCEL_NAME);
                if (parcelable != null && parcelable instanceof Alarm)
                    alarm = (Alarm)parcelable;
            }
        }
        
        return alarm;
    }

    public static Ringtone restoreRingtone(Context context, Bundle bundle, String bundleKey) {
        Ringtone ringtone = null;
        
        if (bundle != null) {
            String ringtoneUriString = bundle.getString(bundleKey);
            
            if (StringUtils.isNotBlank(ringtoneUriString)) {
                Uri ringtoneUri = Uri.parse(ringtoneUriString);
                
                if (ringtoneUri != null) {
                    ringtone = RingtoneManager.getRingtone(context, ringtoneUri);
                }
            }
        }
        
        return ringtone;
    }
    
}
